<template>
  <div class="dialog" v-if="props.isShowMask">
    <Transition name="mask">
      <div class="m-dialog-mask"></div>
    </Transition>
    <div
      :class="['dialogBox', shake ? 'shake' : '']"
      :style="{
        '--top': top + 'px',
      }"
    >
      <div
        class="dialog"
        :style="{
          '--width': width + 'px',
          '--height': height + 'px',
          backgroundColor: bgColor,
        }"
      >
        <div
          :class="['dialogTitle', tilteCenter ? 'tilteCenter' : '']"
          :style="{
            fontSize: titleFontSize + 'px',
            color: titleFontColor,
          }"
        >
          title
        </div>
        <div
          class="dialogContent"
          :style="{
            fontSize: conFontSize + 'px',
            color: conFontColor,
          }"
        >
          <slot></slot>
        </div>

        <div
          :class="['dialogButton', buttonCenter ? 'buttonCenter' : '']"
          :style="{
            display: noButton ? 'none' : 'block',
          }"
        >
          <Button @click="submitDialog" width="80" height="40" class="sub"
            >确认</Button
          >
          <Button
            type="primary"
            @click="closeDialog"
            width="80"
            height="40"
            class="can"
            >取消</Button
          >
        </div>
        <i class="iconfont icon-guanbi guanbi" @click="closeDialog"></i>
      </div>
    </div>
  </div>
</template>

<script lang="ts" scoped>
export default {
  //组件名称
  name: "ls-dialog",
};
</script>
<script lang="ts" setup>
import { withDefaults } from "vue";

import Button from "../Button/Button.vue";

interface Props {
  isShowMask?: boolean /* 是否显示蒙层 */;
  titleFontSize?: string /* 标题字号 */;
  tilteCenter?: boolean /* 标题是否居中 */;
  width?: string /* 对话框宽度 */;
  height?: string /* 对话框高度 */;
  conFontSize?: string /* 文本大小 */;
  buttonCenter?: boolean /*按钮是否居中 */;
  shake?: boolean; // 是否抖动效果
  bgColor?: string /* 背景颜色 */;
  titleFontColor?: string /* 标题颜色 */;
  conFontColor?: string /* 文本颜色 */;
  noButton?: boolean /* 隐藏按钮 */;
  top?: string /* 距离顶部高度 */;
}
/* 设置默认值 */
const props = withDefaults(defineProps<Props>(), {
  isShowMask: false,
  titleFontSize: "28",
  conFontSize: "20",
  tilteCenter: false,
  width: "450",
  height: "220",
  buttonCenter: false,
  shake: false,
  bgColor: "aliceblue",
  titleFontColor: "#181818",
  conFontColor: "#181818",
  noButton: false,
  top: "100",
});
const emit = defineEmits(["closeDialog"]);

const closeDialog = () => {
  emit("closeDialog");
};
const submitDialog = () => {
  emit("closeDialog");
};
</script>

<style lang="scss" scoped>
$width: 450px;
$height: 200px;
$top: 100px;
:root {
  --width: #{$width};
  --height: #{$height};
  --top: #{$top};
}
.mask-enter-active,
.mask-leave-active {
  transition: opacity 0.25s;
}
.mask-enter-from,
.mask-leave-to {
  opacity: 0;
}
.m-dialog-mask {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.45);
}

.dialog {
  .dialogBox {
    position: fixed;
    top: 0;
    inset-inline-end: 0;
    bottom: 0;
    inset-inline-start: 0;
    overflow: auto;
    outline: 0;
    inset: 0;
    z-index: 1010;
    display: flex;
    justify-content: center;
    align-items: center;

    .dialog {
      position: absolute;
      top: var(--top);
      width: var(--width);
      height: var(--height);
      background-color: aliceblue;
      border-radius: 10px;
      .dialogTitle {
        padding-left: 30px;
        padding-top: 10px;
      }
      .dialogContent {
        padding: 20px 30px;
        word-break: break-all;
      }
      .dialogButton {
        position: absolute;
        bottom: 20px;
        right: 25px;
        width: 180px;
        .sub {
          float: left;
        }
        .can {
          float: right;
        }
      }
      .guanbi {
        position: absolute;
        top: 20px;
        right: 30px;
      }
      .buttonCenter {
        left: 50%;
        transform: translate(-50%);
      }
      .tilteCenter {
        text-align: center;
      }
    }
  }
}
.shake {
  animation: shake 500ms;
}
@keyframes shake {
  /* 垂直抖动，核心代码 */
  10%,
  90% {
    transform: translate3d(0, -3px, 0);
  }
  20%,
  80% {
    transform: translate3d(0, +5px, 0);
  }
  30%,
  70% {
    transform: translate3d(0, -6px, 0);
  }
  40%,
  60% {
    transform: translate3d(0, +6px, 0);
  }
  50% {
    transform: translate3d(0, -6px, 0);
  }
}
</style>
